<%@ page import="bean.PollUser, java.sql.*, connection.*, java.util.*" %>
<jsp:useBean id="user" class="bean.PollUser" scope="request" />
<jsp:setProperty name="user" property="*"/>

<%
/*
PollMaster will want quick access to polls they are assigned to manage
*/
DbConnection dbmast = new DbConnection();
Connection connmast = dbmast.getConn();

String username = user.getUsername();
System.out.println(username);
String sql= "SELECT Poll.ID, Poll.Name, Poll.Description FROM Poll, User_Poll " +
	"WHERE User_Poll.Username = '"+username+"' AND "+ 
	"User_Poll.PollID = Poll.ID ";
System.out.println(sql);
PreparedStatement ps = connmast.prepareStatement(sql);
ArrayList<String[]> polls = new ArrayList<String[]>();
boolean hasPolls = false;
try{
	ResultSet rsmast = ps.executeQuery();
	while(rsmast.next()){
		hasPolls = true;
		String temp[] = {"", "", ""};
		temp[0] = rsmast.getString("ID");
		temp[1] = rsmast.getString("NAME");
		temp[2] = rsmast.getString("DESCRIPTION");
		polls.add(temp);
		//temp[3] is reserved for status
	}
}catch(SQLException e){
	System.out.print("PROBLEM ACTUALLY here");
	System.out.print(e.toString());
}


//get participantlists

/*
,IsTemplate	INT
    ,Name		VARCHAR2(40)	
	,MasterUsername	VARCHAR2(40)
	,Username	VARCHAR2(40)
*/
sql= "SELECT DISTINCT LName FROM ParticipantList " +
	"WHERE MasterUsername = '"+username+"'";
System.out.println(sql);
PreparedStatement psmast = connmast.prepareStatement(sql);
ArrayList<String> plists = new ArrayList<String>();
boolean hasPlists = false;
try{
	ResultSet rs = psmast.executeQuery();
	while(rs.next()){
		hasPlists = true;
		String temp = rs.getString("LName");
		plists.add(temp);
	}
}catch(SQLException e){
	System.out.print("PROBLEM here");
	System.out.print(e.toString());
}
%>
<div class="entrytitle">
Poll Management
	<div class="entry">
	Polls you are the master of:
		<%
		if(hasPolls){
			%>
			<div class="tableborder"><table class="blockTable">
			<thead>
			<tr>
				<td>Name</td>
				<td>Description</td>
				<td></td>
				<td></td>
			</tr>
			</thead>
			<tbody>
			<%
			for(String[] curr : polls){%>
				<tr>
					<td>
						<%
						out.write(curr[1]);
						%></td><td><%
						out.write(curr[2]);
						%>
						</td><td>
						<a href="setupPollManage.jsp?location=true&pollid=<%=curr[0]%>&pollName=<%=curr[1]%>">
						<input type="button" class="button"style="display:inline;"  value="Set location"/>
						</a>
						<a href="setupPollManage.jsp?geo=true&pollid=<%=curr[0]%>&pollName=<%=curr[1]%>">
						<input type="button" class="button"style="display:inline;" value="Administer"/>
						</a>
					</td>
					<td>
					</td>
				</tr>
			<%}%>
			</tbody>
			</table></div>
			<%
		}else{
			out.write("You have not been assigned as the master of any polls.<br/>");
		}
		%>
	</div>
</div>
<div class="entrytitle">
Poll Participant Management
	<div class="entry">
	<a href="create_user.jsp"><input type="submit" value="Add a new user" class="button" style="display:inline"/></a>
		<div class="tableborder"><table class="blockTable">
			<thead>
			<tr>
				<td style="width:1px;">List Name</td>
				<td style="width:1px;">Assigned Poll</td>
				<td > </td>
				<td> </td>
			</tr>
			</thead>
			<tbody>
		<%
		if(hasPlists){
			for(String curr : plists){
			//get assigned poll name:
			String pnamesqueEl = "SELECT Poll.name FROM poll, ParticipantList WHERE Poll.id = participantlist.pollid AND participantlist.lname='"+curr+"'";
			System.out.println(pnamesqueEl);
			String assignedPoll = "";
			DbConnection assignDB = new DbConnection();
			try{
				ResultSet rs = assignDB.getConn().prepareStatement(pnamesqueEl).executeQuery();
				if(rs.next()){
					assignedPoll = rs.getString(1);
				}
			}catch(SQLException e){
				System.out.println(e.toString());
				assignedPoll = "";
			}
			%>
			
				<tr <%if(request.getParameter("pmAddList") != null && request.getParameter("pmAddList").equals("true") && request.getParameter("listadd").equals(curr)){ %>class="open" style="background:#c5ffc8" <%}%>>
					<td style="width:1px;" <%if(request.getParameter("pmAddList") != null && request.getParameter("pmAddList").equals("true") && request.getParameter("listadd").equals(curr)){ %>class="open" <%} %>>
						<%
						out.write(curr);
						%>
					</td>
					<td style="width:1px;"><%=assignedPoll %></td>
					<td colspan=2 <%if(request.getParameter("pmAddList") != null && request.getParameter("pmAddList").equals("true") && request.getParameter("listadd").equals(curr)){ %>class="open" style="background:#c5ffc8"<%} %>>
					<a href="PollMaster/setupPollManage.jsp?listname=<%=curr%>">
					<input type="submit"  value="Assign Users" class="button" style="display:inline"/>
					</a>

					<form action="PollMaster/newList.jsp?assignpoll=true"  method="post">
							<input type="submit" value="Assign to poll:" class="button" style="display:inline" />
							<img src="images/arrow_right.gif" style="width:10px;"/>
							<input type="hidden" name="listname" value="<%=curr%>" style="display:inline"/>
							<select name="pollid" style="display:inline" >
							<%if(hasPolls){
							for(String[] listcurr : polls){%>
								<option value="<%=listcurr[0] %>"><%=listcurr[1] %></option>
							<%}
							} %>
							</select>
							<%if(request.getParameter("listpollassigned") != null && request.getParameter("listpollassigned").equals("true") && request.getParameter("assignedpoll").equals(curr)){%><div class="note" style="display:inline;">List assigned to poll!</div><%} %>
					</form>
					
					</td>
				</tr>
			<%}
			}else{
			out.write("<tr><td>You have not created any Participant lists.</td></tr>");
		}%>
			<tr><td colspan="2"style="width:10%;">
					<form action="PollMaster/newList.jsp" method="post">
						<input type="text" name="listname" id="newlist" style="display:inline"/> <input type="submit"  value="+ New List" class="button" style="display:inline"/>
						<%if(request.getParameter("pmAddList") != null && request.getParameter("pmAddList").equals("false") ){ %><div class="error" style="display:inline;">Adding list failed! Name already exists.</div>
						<%}else if(request.getParameter("listpollassigned") != null && request.getParameter("listpollassigned").equals("false")){%><div class="error" style="display:inline;">List assignment failed!</div><%} %>
					</form>
			</td><td></td></tr>
			<%
			
		%>
		</tbody>
			</table></div>
		
	</div>
</div>